package com.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.shop.pojo.PageReq;
import com.web.shop.pojo.PageResp;
import com.web.shop.pojo.RespBean;
import com.web.shop.pojo.TbSpecification;
import com.web.shop.pojogroup.Specification;
import com.web.shop.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/specification-Ms")
public class SpecificationController {

	@Autowired
	private SpecificationService specificationService;
	
   //查询规格
	@RequestMapping("/findAll")
	public List<TbSpecification> findAll(){
		return specificationService.findAll();
	}
   //增加修改规格
	@RequestMapping("/update")
	public RespBean update(@RequestBody Specification specification) {
		if (ObjectUtils.isEmpty(specification.getSpecification().getId())){
			specificationService.add(specification);
			return RespBean.ok("修改成功");
		}else {
			specificationService.update(specification);
			return RespBean.ok("修改成功");
		}

	}

	@RequestMapping("/findOne")
	public Specification findOne(Long id){
		return specificationService.findOne(id);		
	}

	@RequestMapping("/delete/{id}")
	public RespBean delete(@PathVariable Long  id){
			specificationService.delete(id);
			return  RespBean.ok("删除成功");

	}
	//查询+分页
	@GetMapping("/listByPage")
	public PageResp<TbSpecification> listByPage(PageReq pageReq) {
		PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
		Page<TbSpecification> page = (Page<TbSpecification>) specificationService.findAll();
		PageResp pageResp = new PageResp();
		pageResp.setList(page.getResult());
		pageResp.setTotal(page.getTotal());
		return pageResp;
	}

	
}
